package exceptionHandling;

import java.util.Iterator;

import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;
import javax.servlet.http.HttpSession;

import customExceptions.UserLostRightsException;
import dto.RepositoryDTO;

/**
 * Exception handler design to treat .msg Wiki exceptions
 * 
 * @author ciobanm
 * 
 */
public class CustomExceptionHandler extends ExceptionHandlerWrapper {

	private ExceptionHandler wrapped;

	CustomExceptionHandler(ExceptionHandler exception) {
		this.wrapped = exception;
	}

	@Override
	public ExceptionHandler getWrapped() {
		return wrapped;
	}

	/**
	 * 
	 * Main exception handle method.
	 * 
	 */
	@Override
	public void handle() throws FacesException {
		Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents()
				.iterator();

		while (i.hasNext()) {
			ExceptionQueuedEvent event = i.next();
			ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event
					.getSource();

			Throwable exception = context.getException();
			while (exception.getCause() != null) {
				exception = exception.getCause();
			}

			final FacesContext facesContext = FacesContext.getCurrentInstance();

			try {
				
				if (exception instanceof UserLostRightsException) {
					
					UserLostRightsException currentException = (UserLostRightsException) exception;
					treatUserLostRightsException(currentException, facesContext);
				}

			} finally {
				i.remove();
			}

			getWrapped().handle();
		}
	}

	/**
	 * This method discerns between different types of concurrency exceptions
	 * where users loose certain rights.
	 * 
	 * @param exception
	 * @param facesContext
	 */
	private void treatUserLostRightsException(
			UserLostRightsException exception, FacesContext facesContext) {
		final HttpSession sesssion = (HttpSession) facesContext
				.getExternalContext().getSession(false);
		final NavigationHandler nav = facesContext.getApplication()
				.getNavigationHandler();

		switch (exception.getType()) {
		case UserLostRightsException.USER_IS_INVALIDATED:
			
			sesssion.setAttribute("errorMessageNumber", 1);
			nav.handleNavigation(facesContext, null, "/errorLogin.xhtml");
			facesContext.renderResponse();
			break;

		case UserLostRightsException.USER_NO_LONGER_SUPERVISOR:

			sesssion.setAttribute("errorMessageNumber", 2);
			nav.handleNavigation(facesContext, null, "/errorLogin.xhtml");
			facesContext.renderResponse();
			break;

		case UserLostRightsException.USER_HAS_NO_RIGHTS_TO_VIEW_REPOSITORY:

			sesssion.setAttribute("errorMessageNumber", 3);
			nav.handleNavigation(facesContext, null, "/errorLogin.xhtml");
			facesContext.renderResponse();
			break;

		default:
			
			nav.handleNavigation(facesContext, null, "/login.xhtml");
			facesContext.renderResponse();
			break;
		}
	}
}
